import java.sql.*;
import java.util.Scanner;

public class remand {
    public static void add() throws SQLException {
        Scanner sc=new Scanner(System.in);
        System.out.print("用户名为:");String username1=sc.next();
        System.out.print("车辆型号为:");String carname=sc.next();
        System.out.print("厂商为:");String fen_lei=sc.next();

        Connection conn=JDBC.getConnection();
        String sql="INSERT INTO demand(username,carname,fen_lei) VALUES('"+username1+"','"+carname+"','"+fen_lei+"')";
        PreparedStatement ps=null;
        if (conn != null) {
            ps=conn.prepareStatement(sql);
        }
        int set=0;
        if (ps != null) {
            set=ps.executeUpdate();
            System.out.println("添加成功");
        }


    }







    //展示车辆信息
    public static void zhanshi() throws SQLException {

        Connection conn=JDBC.getConnection();

        String sql="SELECT * FROM demand";
        PreparedStatement ps=null;
        if (conn != null) {
            ps=conn.prepareStatement(sql);
        }
        if (ps != null) {
            //执行查询
            ResultSet set=ps.executeQuery();
            ResultSetMetaData rsmd=set.getMetaData();
            System.out.println("(编号，用户名，需求车辆型号，车辆厂商)");
            while(set.next()){
                for(int i=1;i<=rsmd.getColumnCount();i++){
                    System.out.print(set.getObject(i)+"\t");
                }
                System.out.println();
            }
        }



    }








    public static void updata() throws SQLException {
        Scanner sc=new Scanner(System.in);
        System.out.println("要修改需求编号为:");int id=sc.nextInt();

        Connection conn=JDBC.getConnection();

        String sql="DELETE FROM demand WHERE id="+id;
        PreparedStatement ps=null;
        if (conn != null) {
            ps=conn.prepareStatement(sql);
        }
        if (ps != null) {
            ps.executeUpdate();
            System.out.println("修改为:");
            add();
        }else{
            System.out.println("不存在");
        }
    }




    public static void delete() throws SQLException {
        Scanner sc=new Scanner(System.in);
        System.out.println("要删除需求编号为:");int id=sc.nextInt();

        Connection conn=JDBC.getConnection();

        String sql="DELETE FROM demand WHERE id="+id;
        PreparedStatement ps=null;
        if (conn != null) {
            ps=conn.prepareStatement(sql);
        }
        if (ps != null) {
            ps.executeUpdate();
            System.out.println("删除成功");
        }else{
            System.out.println("不存在");
        }

    }






    public static void query() throws SQLException {
        Scanner sc=new Scanner(System.in);
        System.out.println("要查询需求编号为:");int id=sc.nextInt();

        Connection conn=JDBC.getConnection();

        String sql="SELECT * FROM demand WHERE id="+id;
        PreparedStatement ps=null;
        if (conn != null) {
            ps=conn.prepareStatement(sql);
        }
        if (ps != null) {
            ResultSet set=ps.executeQuery();
            ResultSetMetaData rsmd=set.getMetaData();
            System.out.println("(编号，用户名，需求车辆型号，车辆厂商)");
            while(set.next()){
                for(int i=1;i<=rsmd.getColumnCount();i++){
                    System.out.print(set.getObject(i)+"|\t");
                }
                System.out.println();
            }
        }else{
            System.out.println("不存在");
        }

    }





    public static void carneed() throws SQLException {
        System.out.println("现在有以下需求");
        while (true){
            Scanner sc=new Scanner(System.in);
            zhanshi();
            System.out.println("你要执行的操作是:");
            System.out.println("1.添加需求 2.修改需求 3.删除需求 4.查询需求 5.切换车辆页 6.退出系统");
            int choose=sc.nextInt();
            if(choose==1){
                add();
            }else if(choose==2){
                updata();
            }else if(choose==3){
                delete();
            }else if(choose==4){
                query();
            }else if(choose==5){

                used_cars.carzhanshi();
            }else if(choose==6){
                break;
            }else{
                System.out.println("无效操作");
            }
        }
    }

}
